﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sy2_14
{
    public partial class txtResult : Form
    {
        public txtResult()
        {
            InitializeComponent();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            int n, s = 1;
            try
            {
                // 尝试将输入的字符串转换为整数
                n = int.Parse(txtN.Text);
                // 使用 checked 块来检测溢出
                checked
                {
                    for (int i = 1; i <= n; i++)
                    {
                        s *= i;
                    }
                }
                // 显示阶乘结果
                txtResult.Text = s.ToString();
            }
            catch (FormatException)
            {
                // 捕获格式异常（输入不是有效的整数）
                MessageBox.Show("输入格式不合法！", "错误提示");
                txtN.Text = "";
                txtN.Focus();
            }
            catch (OverflowException)
            {
                // 捕获溢出异常（阶乘结果超出整数范围）
                MessageBox.Show("溢出错误！", "错误提示");
                txtN.Text = "";
                txtN.Focus();
            }
            catch (Exception ex)
            {
                // 捕获其他未知异常
                MessageBox.Show("其它异常：" + ex.Message, "错误提示");
                txtN.Text = "";
                txtN.Focus();
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            // 清空输入和结果文本框
            txtN.Text = "";
            textBox2.Text = "";
            txtN.Focus();


        }
    }
}
